package com.java.simple.study.juc.p7;

import com.java.simple.study.tool.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author zhoujunlin
 * @date 2022年08月07日 22:25
 * @desc
 */
@Slf4j(topic = "c.TestSubmit")
public class TestSubmit {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(1);
//        method1(pool);
//        method2(pool);
        //method3(pool);
        pool = Executors.newFixedThreadPool(2);
        testShutDown(pool);
    }

    private static void testShutDown(ExecutorService pool) throws InterruptedException {
        Future<Integer> result1 = pool.submit(() -> {
            log.debug("task1 running...");
            Sleeper.sleep(1);
            log.debug("task1 finish...");
            return 1;
        });

        Future<Integer> result2 = pool.submit(() -> {
            log.debug("task2 running...");
            Sleeper.sleep(1);
            log.debug("task2 finish...");
            return 2;
        });

        Future<Integer> result3 = pool.submit(() -> {
            log.debug("task3 running...");
            Sleeper.sleep(1);
            log.debug("task3 finish...");
            return 3;
        });

        log.debug("shutdown");
        // 等待线程运行完
//        pool.shutdown();
        //pool.awaitTermination(3, TimeUnit.SECONDS);
        //立即关闭  返回等待执行的线程
        List<Runnable> runnables = pool.shutdownNow();
        log.debug("other...{}", runnables);
    }


    private static void method3(ExecutorService pool) throws ExecutionException, InterruptedException {
        // 返回最先执行完成的结果  其余未执行完任务终止执行
        String result = pool.invokeAny(Arrays.asList(
                () -> {
                    log.debug("begin 1");
                    Sleeper.sleep(1);
                    log.debug("end 1");
                    return "1";
                },
                () -> {
                    log.debug("begin 2");
                    Sleeper.sleep(0.5);
                    log.debug("end 2");
                    return "2";
                },
                () -> {
                    log.debug("begin 3");
                    Sleeper.sleep(2);
                    log.debug("end 3");
                    return "3";
                }
        ));
        log.debug("{}", result);
    }

    private static void method2(ExecutorService pool) throws ExecutionException, InterruptedException {
        // 所有任务都执行完  返回
        List<Future<String>> futures = pool.invokeAll(Arrays.asList(
                () -> {
                    log.debug("begin 1");
                    Sleeper.sleep(1);
                    log.debug("end 1");
                    return "1";
                },
                () -> {
                    log.debug("begin 2");
                    Sleeper.sleep(0.5);
                    log.debug("end 2");
                    return "2";
                },
                () -> {
                    log.debug("begin 3");
                    Sleeper.sleep(2);
                    log.debug("end 3");
                    return "3";
                }
        ));

        futures.forEach(future -> {
            try {
                log.debug("{}", future.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });

    }

    private static void method1(ExecutorService pool) throws ExecutionException, InterruptedException {
        Future<String> future = pool.submit(() -> {
            log.debug("begin 1");
            Sleeper.sleep(1);
            log.debug("end 1");
            return "ok";
        });
        log.debug("{}", future.get());
    }

}
